package com.yxb.cms.controller.view.auth;


import com.yxb.cms.architect.constant.BussinessCode;
import com.yxb.cms.architect.utils.BussinessMsgUtil;
import com.yxb.cms.controller.view.BaseController;
import com.yxb.cms.dao.DbAuthManagerMapper;
import com.yxb.cms.domain.dto.BussinessMsg;
import com.yxb.cms.domain.vo.DbAuthManager;
import com.yxb.cms.service.AuthManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * API管理-授权管理
 *
 * @author YXB
 */
@Controller
@RequestMapping(value = "/auth")
public class AuthManagerController extends BaseController {



    @Autowired
    private AuthManagerService authManagerService;

    @Autowired
    private DbAuthManagerMapper authManagerMapper;

    /**
     * 跳转到授权管理页面
     *
     */
    @RequestMapping("/auth_manager_page.action")
    public String toAuthManagerPage() {
        return "auth/auth_manager_page";
    }


    /**
     * 跳转到授权管理新增页面
     *
     * @return
     */
    @RequestMapping("/auth_manager_add.action")
    public String authManagerAdd(Model model) {
        model.addAttribute("pageFlag", "addPage");
        return "auth/auth_manager_edit";
    }

    /**
     * 跳转到授权修改页面
     *
     * @return
     */
    @RequestMapping("/auth_manager_update.action")
    public String authManagerUpdate(Model model,String authId) {
        model.addAttribute("pageFlag", "updatePage");
        DbAuthManager auth = authManagerMapper.selectByPrimaryKey(authId);
        model.addAttribute("auth",auth);
        return "auth/auth_manager_edit";
    }

    /**
     * 保存授权信息
     * @return
     */
    @RequestMapping(value="/ajax_save_auth_manager.action")
    @ResponseBody
    public BussinessMsg ajaxSaveAuthManager(DbAuthManager authManager){
        try {
            return authManagerService.saveOrUpdateAuthManager(authManager,this.getCurrentUserId());
        } catch (Exception e) {
            log.error("保存授权信息信息内部错误{}", e.getMessage(), e);
            return BussinessMsgUtil.returnCodeMessage(BussinessCode.SERVER_SAVE_ERROR);
        }

    }

    /**
     * 删除授权信息
     * @return
     */
    @RequestMapping("/ajax_del_auth_manager.action")
    @ResponseBody
    public BussinessMsg ajaxDelAuthManage(String authId) {
        try {
            return authManagerService.delAuthManager(authId,this.getCurrentUserId());
        } catch (Exception e) {
            log.error("删除授权信息方法内部错误{}",e.getMessage(),e);
            return BussinessMsgUtil.returnCodeMessage(BussinessCode.DELETE_AUTH_MANAGE_ERROR);

        }
    }



    /**
     * 授权信息分页展示
     *
     * @return
     */
    @RequestMapping("/ajax_auth_list.action")
    @ResponseBody
    public Map<String, Object> ajaxAuthList(DbAuthManager authManager) {
        return authManagerService.selectAuthManagerListByPage(authManager);
    }

}
